<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="分类" prop="categoryId">
        <treeselect style="width:300px;" v-model="queryParams.categoryId" :options="categoryList" :show-count="true" placeholder="请选择分类" />
      </el-form-item>
       <el-form-item label="运营商" prop="operatorUserId" v-if="getoperatorfunc(this).userType != 1">
        <el-select v-model="queryParams.operatorUserId" placeholder="运营商" filterable clearable style="width: 200px">
          <el-option
            v-for="dict in operatorList"
            :key="dict.id"
            :label="dict.label"
            :value="dict.id"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['trade:serverCategoryBrokerage:add']"
        >新增
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 表格数据 -->
    <el-table v-loading="loading" :data="list">
      <el-table-column label="分类" align="center" prop="categoryName"/>
      <el-table-column label="运营商用户" align="center" prop="operatorName"/>
      <el-table-column label="佣金百分比" align="center" prop="brokeragePercent"/>
      <el-table-column label="创建时间" align="center" prop="createTime"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="{row}">
          <el-button type="text" icon="el-icon-edit" @click="handleUpdate(row)"
            v-hasPermi="['trade:serverCategoryBrokerage:edit']">修改
          </el-button>
          <el-button type="text" icon="el-icon-delete" @click="handleDelete(row)"
            v-hasPermi="['trade:serverCategoryBrokerage:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改计费规则对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="80%" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="分类" prop="categoryId">
          <treeselect v-model="form.categoryId" :options="categoryList" :show-count="true" placeholder="请选择分类" />
        </el-form-item>
        <el-form-item label="运营商" prop="operatorUserId" v-if="getoperatorfunc(this).userType != 1">
          <el-select v-model="form.operatorUserId" placeholder="运营商" filterable clearable style="width: 200px">
            <el-option
              v-for="dict in operatorList"
              :key="dict.id"
              :label="dict.label"
              :value="Number(dict.id)"
            />
          </el-select>
        </el-form-item>
        <el-form-item label="价格" prop="brokeragePercent">
          <div class="flex_hcneter">
            <el-input-number v-model="form.brokeragePercent" :precision="2" :step="0.1" :min="0"></el-input-number>
            <div style="margin-left:10px;">%</div>
          </div>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :loading="popLodding" @click="submitForm">确 定</el-button>
        <el-button @click="open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import * as api from "@/api/commodity/categOperatComm";
import { treeListServerCategory } from "@/api/commodity/serviceClass";
import { listAllOperator } from "@/api/operator/operatorList";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
name: "CategOperatComm",
dicts: ['comm_yesno_status',],
components: { Treeselect, },
data() {
  return {
    popLodding:false,//弹窗加载
    form:{},
    // 弹出层标题
    title: "",
    // 是否显示弹出层
    open: false,
    // 遮罩层
    loading: true,
    // 显示搜索条件
    showSearch: true,
    queryParams: {// 查询参数
      pageNum: 1,certificateName:null,status:null,
      pageSize: 10,
    },
    // 总条数
    total: 0,
    rules: {
      brokeragePercent: [{required: true, message: "佣金百分比不能为空", trigger: "blur"}],
      operatorUserId: [{required: true, message: "运营商用户不能为空", trigger: "blur"}],
      categoryId: [{required: true, message: "分类不能为空", trigger: "change"}],
    },
    list:[],//页面数据
    operatorList:[],//运营商数据
    categoryList:[],//分类数据
  };
},
created() {
  this.treeListServerCategory()
  this.listAllOperator()
  this.getList()
},
methods: {
  async treeListServerCategory(){//查询分类树列表
    let res = await treeListServerCategory()
    let data = res.data || []
    this.categoryList = data
    this.findChild(this.categoryList)
  },
  findChild(list){//找到是否存在
    list.forEach(item=>{
      item.label = item.name
      let children = item.children || []
      if(children.length){
        this.findChild(children)
      }else{
        delete item.children
      }
    })
  },
  async listAllOperator(){//获取运营商
    let res =await listAllOperator()
    let data = res.data || []
    this.operatorList = data
  },
  getList() {
    this.loading = true;
    api.listServerCategoryBrokerage(this.queryParams).then(response => {
      this.list = response.rows || [];
      this.total = response.total || 0;
      this.loading = false;
    });
  },
  handleAdd() {
    this.open = true;
    this.reset();
    this.title = "添加分类运营商佣金";
  },
  handleUpdate(row) {
    this.reset();
    const id = row.id || this.ids;
    this.open = true;
    this.title = "修改分类运营商佣金";
    api.getServerCategoryBrokerage(id).then(response => {
      let data = response.data || {}
      this.form = {...data};
    });
  },
  submitForm() {
    this.$refs["formRef"].validate(valid => {
      if (valid) {
        let form = {...this.form}
        this.popLodding = true
        if(this.getoperatorfunc(this).operatorUserId) delete form.operatorUserId
        if (this.form.id != undefined) {
          api.updateServerCategoryBrokerage(form).then(response => {
            this.popLodding = false
            this.$modal.msgSuccess("修改成功")
            this.open = false;
            this.getList();
          }).catch(error=>this.popLodding = false);
        } else {
          api.addServerCategoryBrokerage(form).then(response => {
            this.popLodding = false
            this.$modal.msgSuccess("新增成功")
            this.open = false;
            this.getList()
          }).catch(error=>this.popLodding = false);
        }
      }
    });
  },
  // 表单重置
  reset() {
    this.form = {status:1,operatorUserId:this.getoperatorfunc(this).operatorUserId};
    this.resetForm("formRef");
    this.treeListServerCategory()
  },
  /** 搜索按钮操作 */
  handleQuery() {
    this.queryParams.pageNum = 1;
    this.getList();
  },
  /** 重置按钮操作 */
  resetQuery() {
    this.resetForm("queryForm");
    this.handleQuery();
  },
  handleDelete(row) {//删除
    const del_ids = row.id || this.ids;
    this.$modal.confirm('是否确认删除该数据项？').then(function() {
      return api.delServerCategoryBrokerage(del_ids);
    }).then(() => {
      this.getList();
      this.$modal.msgSuccess("删除成功");
    }).catch(() => {});
  },
}
};
</script>
